package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.SettlementMethod;
import com.qianying.service.SettlementMethodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 结算方式控制器
 * 所有接口参数通过Swagger的@Parameter显式定义，与实体类Schema对应
 */
@RestController
@RequestMapping("/admin/settlementMethod")
@Tag(name = "结算方式管理（结算方式设置）", description = "结算方式的增删改查接口")
public class SettlementMethodController {

    @Autowired
    private SettlementMethodService settlementMethodService;

    /**
     * 分页查询结算方式列表
     */
    @PostMapping("/getPage")
    @Operation(
            summary = "分页查询结算方式列表",
            description = "支持多条件筛选，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码，默认1", example = "1", required = false),
                    @Parameter(name = "size", description = "每页条数，默认10", example = "10", required = false),
                    @Parameter(name = "methodCode", description = "结算方式代码，唯一标识", example = "01", required = false),
                    @Parameter(name = "methodName", description = "结算方式名称", example = "现结", required = false),
                    @Parameter(name = "periodDays", description = "账期天数，0表示非账期", example = "30", required = false),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1", required = false)
            }
    )
    public CommonResult<IPage<SettlementMethod>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody(required = false) SettlementMethod settlementMethod) {
        Page<SettlementMethod> page = new Page<>(current, size);
        return settlementMethodService.getPage(page, settlementMethod == null ? new SettlementMethod() : settlementMethod);
    }

    /**
     * 根据ID查询结算方式详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询结算方式详情",
            description = "通过ID获取结算方式完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", example = "1", required = true)
            }
    )
    public CommonResult<SettlementMethod> getById(@RequestParam Long id) {
        return settlementMethodService.getById(id);
    }

    /**
     * 新增结算方式
     */
    @PostMapping("/saveMethod")
    @Operation(
            summary = "新增结算方式",
            description = "创建新的结算方式记录",
            parameters = {
                    @Parameter(name = "methodCode", description = "结算方式代码，唯一", example = "01", required = true),
                    @Parameter(name = "methodName", description = "结算方式名称，唯一", example = "现结", required = true),
                    @Parameter(name = "periodDays", description = "账期天数，0表示非账期", example = "30", required = false),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用），默认1", example = "1", required = false),
                    @Parameter(name = "description", description = "结算方式描述信息", example = "订单完成后立即支付", required = false)
            }
    )
    public CommonResult<?> saveMethod(@RequestBody SettlementMethod settlementMethod) {
        return settlementMethodService.saveMethod(settlementMethod);
    }

    /**
     * 更新结算方式
     */
    @PostMapping("/updateMethod")
    @Operation(
            summary = "更新结算方式",
            description = "修改已有结算方式信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", example = "1", required = true),
                    @Parameter(name = "methodCode", description = "结算方式代码，唯一", example = "01", required = true),
                    @Parameter(name = "methodName", description = "结算方式名称，唯一", example = "现结", required = true),
                    @Parameter(name = "periodDays", description = "账期天数，0表示非账期", example = "30", required = false),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1", required = false),
                    @Parameter(name = "description", description = "结算方式描述信息", example = "订单完成后立即支付", required = false)
            }
    )
    public CommonResult<?> updateMethod(@RequestBody SettlementMethod settlementMethod) {
        return settlementMethodService.updateMethod(settlementMethod);
    }

    /**
     * 更新结算方式状态
     */
    @PostMapping("/updateStatus")
    @Operation(
            summary = "更新结算方式状态",
            description = "单独更新结算方式的启用/禁用状态",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", example = "1", required = true),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1", required = true)
            }
    )
    public CommonResult<?> updateStatus(
            @RequestParam Long id,
            @RequestParam Integer status) {
        return settlementMethodService.updateStatus(id, status);
    }

    /**
     * 删除结算方式
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除结算方式",
            description = "通过ID删除指定结算方式",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", example = "1", required = true)
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return settlementMethodService.removeById(id);
    }

    /**
     * 查询所有结算方式
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有结算方式",
            description = "返回所有结算方式列表，适用于下拉选择"
    )
    public CommonResult<List<SettlementMethod>> getAll() {
        return settlementMethodService.getAll();
    }
}
